<template>
  <div class="menu-head">
    <div class="logo-box" @click="router.push('/')">
        <img
          class="info-img"
          src="https://arkpaastest.analysys.cn:4005/images/ubalogo.eb566fb3.svg"/>
        <span class="info-name">{{appName}}</span>
    </div>
    <div class="nav-box">
      <div class="nav-item">
        <router-link class="item-href" :class="{ act: getAct('/dashboard') }" to="/dashboard">看板</router-link>
      </div>
      <div class="nav-item">
        <router-link class="item-href" :class="{ act: getAct('/make') }" to="/module"
          >图表
        </router-link>
      </div>
      <div class="nav-item">
        <a-popover overlayClassName="nav-user-pop">
          <template #content>
            <div class="user-info-item" @click="router.replace('/user?type=kanban')">我的看板</div>
            <div class="user-info-item" @click="router.replace('/user?type=module')">我的图表</div>
          </template>
          <router-link class="item-href" to="/user">我的 <CaretDownOutlined /></router-link>
        </a-popover>
      </div>
    </div>
    <div class="nav-right">
      <div class="nav-item">
        <a-popover overlayClassName="nav-user-pop" placement="bottomRight">
          <a-avatar>
            <template #icon>
              <UserOutlined />
            </template>
          </a-avatar>
          <template #content>
            <div>
              <div v-if="userInfo">
                <div class="nav-user-info">
                  <a-avatar size="large">
                    <template #icon>
                      <UserOutlined />
                    </template>
                  </a-avatar>
                  <span class="user-name">{{ userInfo.username }}</span>
                </div>
                <router-link to="/user"
                  ><div class="user-info-item"><UserOutlined /> 个人中心</div></router-link
                >
                <div class="user-info-item" @click="logout()"><LoginOutlined /> 退出登录</div>
              </div>
              <div style="text-align: center; padding: 10px 20px" v-else>
                未登录, <span class="primary-color" @click="login()">点击登录</span>
              </div>
            </div>
          </template>
        </a-popover>
      </div>
    </div>
  </div>
</template>

<script>
  import { CaretDownOutlined, UserOutlined, LoginOutlined } from '@ant-design/icons-vue';
  import { defineComponent, ref, reactive, computed } from 'vue';
  import io from '@/utils/http/index.js';
  import { useStore } from 'vuex';
  import { useRouter, useRoute } from 'vue-router';

  export default defineComponent({
    components: { CaretDownOutlined, UserOutlined, LoginOutlined },

    setup() {
      const store = useStore();
      const route = useRoute(),
        router = useRouter();

      const state = reactive({});

      const getAct = (path) => {
        return route.path.indexOf(path) > -1;
      };

      const logout = () => {
        io({
          url: '/show/user/logout',
        }).then((res) => {
          window.location.href = '/';
        });
      };

      const login = () => {
        store.commit('common/setParams', { loginDisplay: true });
      };

      return {
        getAct,
        userInfo: computed(() => store.state.user.userInfo),
        logout,
        login,
        router,
        appName: import.meta.env.VITE_APP_NAME
      };
    },
  });
</script>
<style lang="scss">
  .menu-head {
    position: relative;
    .logo-box {
      height: 54px;
      line-height: 54px;
      cursor: pointer;
      width: 200px;
      float: left;
      .info-img {
        max-width: 24px;
        max-height: 24px;
        vertical-align: sub;
        margin: 0 6px 0 25px;
      }
      .info-name {
        font-size: 16px;
        font-weight: 500;
      }
    }
    .nav-box {
      float: left;
      font-size: 15px;
      height: 54px;
      .nav-item {
        float: left;
        height: 54px;
        line-height: 54px;
        margin-right: 20px;
        cursor: pointer;
        .item-href {
          padding: 0 20px;
          display: block;
          &:hover {
            background-color: #f9f9f9;
            color: $primary-color;
          }
          &.act {
            color: $primary-color;
          }
        }
        .anticon {
          font-size: 12px;
          color: #c0ccda;
          margin-left: 3px;
        }
      }
    }
    .nav-right {
      position: absolute;
      right: 20px;
      top: 0;
      height: 100%;
      .nav-item {
        float: right;
        height: 54px;
        line-height: 54px;
        margin-left: 20px;
        cursor: pointer;
      }
    }
  }
  .nav-user-pop {
    .ant-popover-inner-content {
      padding: 0;
      padding-bottom: 10px;
      padding-top: 10px;
      min-width: 110px;
    }
  }
  .nav-user-info {
    width: 200px;
    padding: 20px;
    padding-top: 10px;
    .user-name {
      font-size: 16px;
      font-weight: 500;
      margin-left: 10px;
    }
  }
  .user-info-item {
    line-height: 36px;
    cursor: pointer;
    padding-left: 20px;
    &:hover {
      background-color: #f9f9f9;
      color: $primary-color;
    }
  }
</style>
